package com.size.mall.util;

import io.jsonwebtoken.*;
import lombok.experimental.UtilityClass;

import java.util.Map;

@UtilityClass
public class JwtUtil {

    public String encode(String key, Map<String, Object> param, String salt) {
        if (salt != null) {
            key += salt;
        }

        JwtBuilder jwtBuilder = Jwts.builder().signWith(SignatureAlgorithm.HS256, key);

        jwtBuilder.setClaims(param);
        String token = jwtBuilder.compact();
        return token;


    }


    public Map<String, Object> decode(String token, String key, String salt) {
        Claims claims = null;
        if (salt != null) {
            key += salt;
        }
        try {
            claims = Jwts.parser().setSigningKey(key).parseClaimsJws(token).getBody();
        } catch (JwtException e) {
            return null;
        }
        return claims;
    }


}
